package com.kgcx.common.bean;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.kgcx.common.constant.Cons;
import com.kgcx.common.utils.IpUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class RequestData extends HashMap {


    private static final long serialVersionUID = 1L;

    HttpServletRequest request;

    public RequestData(HttpServletRequest request) {
        this.request = request;
        Map properties = request.getParameterMap();
        Iterator entries = properties.entrySet().iterator();
        Entry entry;
        while (entries.hasNext()) {
            String name = "";
            String value = "";
            entry = (Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = value + values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            this.put(name, value);
        }
        buildCommonParam();

    }


    public RequestData() {
        this(true);
    }

    public RequestData(boolean initCommonParamFlag) {
        if (initCommonParamFlag) {
            buildCommonParam();
        }
    }

    public void genIpAndPort() {
        this.put("ip", IpUtils.getIpAddr(request));
        this.put("port", String.valueOf(request.getRemotePort()));
    }

    private void buildCommonParam() {
        this.put("COMMON_STATUS_ACTIVE", Cons.COMMON_STATUS_ACTIVE);
    }

    public Integer getPageNum() {
        return getInteger("pageNum");
    }

    public Integer getPageSize() {
        return getInteger("pageSize");
    }

    public String getOrderBy() {
        return getString("orderBy");
    }


    public String getString(Object key) {
        if (null != get(key)) {
            return get(key).toString();
        } else {
            return null;
        }
    }

    public String getStringOrDefault(Object key, String defaultValue) {
        if (null != get(key)) {
            return (String) get(key);
        } else {
            return defaultValue;
        }
    }


    public Integer getInteger(Object key) {
        return TypeUtils.castToInt(get(key));
    }


    public Long getLong(Object key) {
        return TypeUtils.castToLong(get(key));
    }

    public Float getFloat(Object key) {
        return TypeUtils.castToFloat(get(key));
    }

    public Timestamp getTimestamp(Object key) {
        return TypeUtils.castToTimestamp(get(key));
    }

    @Override
    public String toString() {
        Object clone = this.clone();
        Map m = (Map) clone;
        m.remove("COMMON_STATUS_ACTIVE");
        return JSONObject.toJSONString(m);
    }

}
